﻿using DAL;
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using Tools;
using 纺织贸易管理系统;

namespace BLL
{
    public static class 库存BLL
    {
        public static string ClearStockRull { get; set; } = string.Empty;
        public static danjumingxitable 获取入库明细(StockTable item)
        {
            return Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == item.RKDH &&
            x.yanse == item.YS &&
            x.pingming == item.PM &&
            x.ganghao == item.GH &&
            x.OrderNum == item.orderNum
            );
        }
        //辅料出入库
        public static void 增加库存(List<AccessoriesInfo> danjumingxitables, DanjuTable danjuTable)
        {

            foreach (var mingxi in danjumingxitables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.ID == mingxi.suolv);
                if (stock.ID == 0)
                {
                    stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.YS == mingxi.yanse &&
                   x.kuanhao == mingxi.kuanhao && x.GH == mingxi.ganghao && x.CustomName == mingxi.CustomName && x.PM == mingxi.pingming);
                    //库存没有这个布  当编号为空的时候
                    if (stock.ID == 0)
                    {
                        stock = new StockTable
                        {
                            AvgPrice = mingxi.hanshuidanjia + mingxi.AveragePrice,
                            BH = mingxi.Bianhao,
                            biaoqianmishu = 0,
                            CF = mingxi.chengfeng,
                            CKMC = danjuTable.ckmc,
                            ContractNum = mingxi.ContractNum,
                            CustomName = mingxi.CustomName,
                            GG = mingxi.guige,
                            GH = mingxi.ganghao,
                            houzhengli = mingxi.houzhengli,
                            JS = mingxi.chengpingjuanshu,
                            kaijianmishu = 0,
                            kuanhao = mingxi.kuanhao,
                            Kuwei = mingxi.Kuwei,
                            KZ = mingxi.kezhong,
                            MF = mingxi.menfu,
                            MS = mingxi.chengpingmishu,
                            orderNum = mingxi.OrderNum,
                            own = danjuTable.own,
                            PM = mingxi.pingming,
                            RKDH = mingxi.rkdh,
                            RQ = danjuTable.rq,
                            TotalMoney = mingxi.hanshuiheji,
                            YS = mingxi.yanse,
                            Huahao = mingxi.Huahao,
                            ColorNum = mingxi.ColorNum,
                            CustomerColorNum = mingxi.CustomerColorNum,
                            CustomerPingMing = mingxi.CustomerPingMing,
                            Rangchang = mingxi.Rangchang,
                            RukuNum = mingxi.toupimishu,
                            YiFaNum = 0
                        };
                        if (!string.IsNullOrEmpty(mingxi.offerid))
                        {
                            var liuzhuanka = LiuzhuancardService.GetOneLiuzhuancard(x => x.CardNum == mingxi.offerid);
                            stock.MaShu = liuzhuanka.MaShu;
                            stock.ShippingMark = liuzhuanka.ShippingMark;
                            stock.houzhengli = liuzhuanka.Beizhu;
                            var juans = JuanHaoTableService.GetJuanHaoTablelst(x => x.Ckmc == mingxi.suolv.ToString());
                            stock.yijianmishu = juans.Sum(x => x.MiShu);
                            stock.yijianjuanshu = juans.Count;
                            stock.biaoqianmishu = juans.Sum(x => x.biaoqianmishu);
                            stock.LiuzhuanCard = liuzhuanka.CardNum;
                        }
                        var m = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == mingxi.rkdh && x.ganghao == mingxi.ganghao && x.yanse == mingxi.yanse);
                        if (m != null)
                        {
                            stock.MS = m.chengpingmishu;
                            stock.JS = m.chengpingjuanshu;
                            stock.IsCheckDone = "未完成";
                            stock.RukuNum = m.chengpingmishu;
                        }
                        Connect.dbcontext.Insertable(stock).ExecuteCommand();
                    }
                    //如果已经存在
                    else
                    {
                        stock.MS += danjuTable.djlx == DanjuLeiXing.销售出库单 ? mingxi.toupimishu : mingxi.chengpingmishu;
                        stock.JS += mingxi.chengpingjuanshu;
                        stock.RukuNum += mingxi.toupimishu;
                        stock.Difference = stock.RukuNum - stock.yijianmishu;
                        if (danjuTable.djlx == DanjuLeiXing.销售出库单)
                        {
                            stock.YiFaNum -= mingxi.toupimishu;
                        }
                        Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                    }
                }
                else
                {
                    stock.MS += danjuTable.djlx == DanjuLeiXing.销售出库单 ? mingxi.toupimishu : mingxi.chengpingmishu;
                    stock.JS += mingxi.chengpingjuanshu;
                    stock.RukuNum += mingxi.toupimishu;
                    stock.Difference = stock.RukuNum - stock.yijianmishu;
                    if (danjuTable.djlx == DanjuLeiXing.销售出库单)
                    {
                        stock.YiFaNum -= mingxi.toupimishu;
                    }
                    Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                }
                //增加订单明细的库存数(mingxi, danjuTable.ckmc);
            }
        }

        public static void 减少库存(List<AccessoriesInfo> danjumingxitables, DanjuTable danjuTable)
        {
            foreach (var mingxi in danjumingxitables)
            {
                var stock = Connect.dbcontext.Queryable<StockTable>().First(x => x.ID == mingxi.suolv);
                if (stock == null)
                {
                    stock = Connect.dbcontext.Queryable<StockTable>().First(x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.YS == mingxi.yanse &&
                   x.kuanhao == mingxi.kuanhao && x.GH == mingxi.ganghao && x.CustomName == mingxi.CustomName && x.PM == mingxi.pingming);
                    //库存没有这个布  当编号为空的时候
                    if (stock != null)
                    {
                    //    stock = new StockTable();
                    //    stock.AvgPrice = mingxi.hanshuidanjia;
                    //    stock.BH = mingxi.Bianhao;
                    //    stock.biaoqianmishu = 0;
                    //    stock.CF = mingxi.chengfeng;
                    //    stock.CKMC = danjuTable.ckmc;
                    //    stock.ContractNum = mingxi.ContractNum;
                    //    stock.CustomName = mingxi.CustomName;
                    //    stock.GG = mingxi.guige;
                    //    stock.GH = mingxi.ganghao;
                    //    stock.houzhengli = mingxi.houzhengli;
                    //    stock.JS = 0 - mingxi.chengpingjuanshu;
                    //    stock.kaijianmishu = 0;
                    //    stock.kuanhao = mingxi.kuanhao;
                    //    stock.Kuwei = mingxi.Kuwei;
                    //    stock.KZ = mingxi.kezhong;
                    //    stock.MF = mingxi.menfu;
                    //    stock.MS = 0 - mingxi.chengpingmishu;
                    //    stock.orderNum = mingxi.OrderNum;
                    //    stock.own = danjuTable.own;
                    //    stock.PM = mingxi.pingming;
                    //    stock.RKDH = mingxi.danhao;
                    //    stock.RQ = danjuTable.rq;
                    //    stock.TotalMoney = 0 - mingxi.hanshuiheji;
                    //    stock.YS = mingxi.yanse;
                    //    stock.Huahao = mingxi.Huahao;
                    //    stock.ColorNum = mingxi.ColorNum;
                    //    stock.CustomerColorNum = mingxi.CustomerColorNum;
                    //    stock.CustomerPingMing = mingxi.CustomerPingMing;
                    //    stock.Rangchang = mingxi.Rangchang;
                    //    stock.RKDH = danjuTable.ksmc;
                    //    stock.Difference = stock.MS - stock.biaoqianmishu;
                    //    Connect.dbcontext.Insertable(stock);
                    //}   //如果已经存在
                    //else
                    //{
                        if (stock.yijianmishu == 0)
                        {
                            stock.MS -= danjuTable.djlx == DanjuLeiXing.销售出库单 ? mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m) : mingxi.chengpingmishu;
                            stock.JS -= mingxi.chengpingjuanshu;
                            if (danjuTable.djlx == DanjuLeiXing.销售出库单)
                            {
                                stock.YiFaNum += mingxi.toupimishu;
                            }
                            Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                        }
                    }

                }   //如果已经存在
                else
                {
                    if (stock.yijianmishu == 0)
                    {
                        stock.MS -= mingxi.danwei == "米" ? mingxi.chengpingmishu : Math.Round(mingxi.chengpingmishu * 0.9144m)  ;
                        stock.JS -= mingxi.chengpingjuanshu;
                        if (danjuTable.djlx == DanjuLeiXing.销售出库单)
                        {
                            stock.YiFaNum += mingxi.toupimishu;
                        }
                        if (stock.MS <= 0)
                        {
                            RukuTableService.InsertRukuTable(stock);
                            Connect.dbcontext.Deleteable<StockTable>().Where(x => x.ID == stock.ID).ExecuteCommand();
                        }
                        else
                        {
                            Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                        }
                    }
                }

                if (mingxi.IsDone)
                {
                    if (Connect.dbcontext.Queryable<JuanHaoTable>().Where(x => x.Ckmc == stock.ID.ToString() && x.state == 0).ToList().Count == 0)
                    {
                            if (stock != null)
                            {
                                RukuTableService.InsertRukuTable(stock);
                                Connect.dbcontext.Deleteable<StockTable>().Where(x => x.ID == stock.ID).ExecuteCommand();
                            }
                    }
                }
            }
        }
        /// <summary>
        /// 面料入库
        /// </summary>
        /// <param name="danjumingxitables"></param>
        /// <param name="danjuTable"></param>
        public static void 增加库存(List<danjumingxitable> danjumingxitables, DanjuTable danjuTable)
        {
            foreach (var mingxi in danjumingxitables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.ID == mingxi.suolv);
                if (stock.ID == 0)
                {
                    stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.YS == mingxi.yanse &&
                   x.kuanhao == mingxi.kuanhao && x.GH == mingxi.ganghao && x.CustomName == mingxi.CustomName && x.PM == mingxi.pingming&&x.RKDH==mingxi.rkdh&&x.LiuzhuanCard==mingxi.offerid );
                    //库存没有这个布  当编号为空的时候
                    if (stock.ID == 0)
                    {
                        stock = new StockTable
                        {
                            AvgPrice = mingxi.hanshuidanjia + mingxi.AveragePrice,
                            BH = mingxi.Bianhao,
                            biaoqianmishu = 0,
                            CF = mingxi.chengfeng,
                            CKMC = danjuTable.ckmc,
                            ContractNum = mingxi.ContractNum,
                            CustomName = mingxi.CustomName,
                            GG = mingxi.guige,
                            GH = mingxi.ganghao,
                            houzhengli = mingxi.houzhengli,
                            JS = mingxi.chengpingjuanshu,
                            kaijianmishu = 0,
                            kuanhao = mingxi.kuanhao,
                            Kuwei = mingxi.Kuwei,
                            KZ = mingxi.kezhong,
                            MF = mingxi.menfu,
                            MS =mingxi.danwei=="米"? mingxi.toupimishu:Math.Round( mingxi.toupimishu*0.9144m),
                            orderNum = mingxi.OrderNum,
                            own = danjuTable.own,
                            PM = mingxi.pingming,
                            RKDH = mingxi.rkdh,
                            RQ = danjuTable.rq,
                            TotalMoney = mingxi.hanshuiheji,
                            YS = mingxi.yanse,
                            Huahao = mingxi.Huahao,
                            ColorNum = mingxi.ColorNum,
                            CustomerColorNum = mingxi.CustomerColorNum,
                            CustomerPingMing = mingxi.CustomerPingMing,
                            Rangchang = mingxi.Rangchang,
                            RukuNum = mingxi.toupimishu,
                            YiFaNum = 0
                        };
                        if (!string.IsNullOrEmpty(mingxi.offerid))
                        {
                            var liuzhuanka = LiuzhuancardService.GetOneLiuzhuancard(x => x.CardNum == mingxi.offerid);
                            stock.MaShu = liuzhuanka.MaShu;
                            stock.ShippingMark = liuzhuanka.ShippingMark;
                            stock.houzhengli = liuzhuanka.Beizhu;
                            var juans = JuanHaoTableService.GetJuanHaoTablelst(x => x.Ckmc == mingxi.suolv.ToString());
                            stock.yijianmishu = juans.Sum(x => x.MiShu);
                            stock.yijianjuanshu = juans.Count;
                            stock.biaoqianmishu = juans.Sum(x => x.biaoqianmishu);
                            stock.LiuzhuanCard = liuzhuanka.CardNum;
                        }
                        var m = Connect.dbcontext.Queryable<RukuTable>().First(x => x.RKDH == mingxi.rkdh && x.GH == mingxi.ganghao && x.YS == mingxi.yanse&&x.CKMC==danjuTable.ckmc);
                        if (m != null)
                        {
                            stock.IsCheckDone = m.IsCheckDone;
                            stock.RukuNum = m.RukuNum;
                            stock.MaShu = m.MaShu;
                            stock.ShippingMark = m.ShippingMark;
                            stock.houzhengli = m.houzhengli;                       
                            stock.yijianmishu = m.yijianmishu;
                            stock.yijianjuanshu =m.yijianjuanshu;
                            stock.biaoqianmishu =m.biaoqianmishu;
                            stock.LiuzhuanCard = m.LiuzhuanCard ;
                            stock.JS = m.JS+mingxi.chengpingjuanshu;
                            stock.MS = m.MS +( mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m));
                        }
                        else
                        {
                           var  r = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == mingxi.rkdh && x.ganghao == mingxi.ganghao && x.yanse == mingxi.yanse);
                            if (r != null)
                            {
                                stock.MS = r.chengpingmishu;
                                stock.JS = r.chengpingjuanshu;
                                stock.IsCheckDone = "未完成";
                                stock.RukuNum = r.chengpingmishu;
                            }
                        }
                        StockTableService.InsertStockTable(stock);
                    }
                    //如果已经存在
                    else
                    {
                        stock.MS += danjuTable.djlx == DanjuLeiXing.销售出库单 ?mingxi.danwei=="米"? mingxi.toupimishu:Math.Round ( mingxi.toupimishu*0.9144m) : mingxi.chengpingmishu;
                        stock.JS += mingxi.chengpingjuanshu;
                        stock.RukuNum += mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m);
                        stock.Difference = stock.RukuNum - stock.yijianmishu;
                        if (danjuTable.djlx == DanjuLeiXing.销售出库单)
                        {
                            stock.YiFaNum -= mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m);
                        }
                        Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                    }
                }
                else
                {
                    stock.MS += danjuTable.djlx == DanjuLeiXing.销售出库单 ? mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m) : mingxi.chengpingmishu;
                    stock.JS += mingxi.chengpingjuanshu;
                    stock.RukuNum += mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m);
                    stock.Difference = stock.RukuNum - stock.yijianmishu;
                    if (danjuTable.djlx == DanjuLeiXing.销售出库单)
                    {
                        stock.YiFaNum -= mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m);
                    }
                    Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                }
                //增加订单明细的库存数(mingxi, danjuTable.ckmc);
            }
        }
        
        /// <summary>
        /// 面料出库
        /// </summary>
        /// <param name="danjumingxitables"></param>
        /// <param name="danjuTable"></param>
        public static void 减少库存(List<danjumingxitable> danjumingxitables, DanjuTable danjuTable)
        {
            foreach (var mingxi in danjumingxitables)
            {
                var stock = Connect.dbcontext.Queryable<StockTable>().First(x => x.ID == mingxi.suolv);
                if (stock == null)
                {
                    stock = Connect.dbcontext.Queryable<StockTable>().First(x => x.CKMC == danjuTable.ckmc  && x.YS == mingxi.yanse 
                   && x.GH == mingxi.ganghao && x.CustomName == mingxi.CustomName && x.PM == mingxi.pingming&&x.RKDH==mingxi.rkdh );
                    //库存没有这个布  当编号为空的时候
                    if (stock!= null)
                    {
                        if (stock.yijianmishu == 0)
                        {
                            stock.MS -= danjuTable.djlx == DanjuLeiXing.销售出库单 ? mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m) : mingxi.chengpingmishu;
                            stock.JS -= mingxi.chengpingjuanshu;
                            if (danjuTable.djlx == DanjuLeiXing.销售出库单)
                            {
                                stock.YiFaNum += mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m);
                            }
                            Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                        }
                    }
                    else
                    {
                        if(!string.IsNullOrEmpty(mingxi.rkdh))
                        {
                            throw new Exception("没有库存！请核对");
                        }
                    }
                }   //如果已经存在
                else
                {                   
                        stock.MS -= danjuTable.djlx == DanjuLeiXing.销售出库单 ? mingxi.danwei == "米" ? mingxi.toupimishu : Math.Round(mingxi.toupimishu * 0.9144m) : mingxi.chengpingmishu;
                        stock.JS -= mingxi.chengpingjuanshu;
                        if (danjuTable.djlx == DanjuLeiXing.销售出库单)
                        {
                            stock.YiFaNum += mingxi.toupimishu;
                        }
                        Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                }

                if (stock != null)
                {
                    if (JuanHaoTableService.GetJuansWithStock(stock).Count == 0)
                    {
                        if (danjuTable.djlx != DanjuLeiXing.销售出库单)
                        {
                            if (stock.MS == 0 && stock.JS == 0)
                            {
                                //Connect.dbcontext.Insertable(stock).ExecuteCommand();
                                Connect.dbcontext.Deleteable<StockTable>(stock).ExecuteCommand();
                            }
                        }
                        if (mingxi.IsDone)
                        {
                            if (Connect.dbcontext.Queryable<JuanHaoTable>().Where(x => x.Ckmc == stock.ID.ToString() && x.state == 0).ToList().Count == 0)
                            {
                                if (!QueryTime.ManualInventoryClearance)
                                {
                                    if (stock != null)
                                    {
                                        RukuTableService.InsertRukuTable(stock);
                                        Connect.dbcontext.Deleteable<StockTable>().Where(x => x.ID == stock.ID).ExecuteCommand();
                                    }
                                }
                                else
                                {
                                    if (stock != null)
                                    {
                                        if (stock.IsCheckDone != null && stock.IsCheckDone.Contains("已"))
                                        {
                                            RukuTableService.InsertRukuTable(stock);
                                            Connect.dbcontext.Deleteable<StockTable>().Where(x => x.ID == stock.ID).ExecuteCommand();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 减少生产入库库存
        /// </summary>
        /// <param name="danjumingxitables"></param>
        /// <param name="danjuTable"></param>
        public static void 减少入库库存(List<danjumingxitable> danjumingxitables, DanjuTable danjuTable)
        {
            foreach (var mingxi in danjumingxitables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.ckmc && x.YS == mingxi.yanse &&
                 x.GH == mingxi.ganghao && x.PM == mingxi.pingming && x.CustomName == danjuTable.ksmc && x.RKDH == mingxi.rkdh);
                if (stock.ID == 0)
                {
                    stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.ckmc && x.YS == mingxi.yanse &&
                   x.GH == mingxi.ganghao && x.PM == mingxi.pingming && x.CustomName == danjuTable.ksmc);
                }
                //库存没有这个布  当编号为空的时候
                if (stock.ID == 0)
                {
                    stock.AvgPrice = mingxi.hanshuidanjia;
                    stock.BH = mingxi.Bianhao;
                    stock.biaoqianmishu = 0;
                    stock.CF = mingxi.chengfeng;
                    stock.CKMC = danjuTable.ckmc;
                    stock.ContractNum = mingxi.ContractNum;
                    stock.CustomName = mingxi.CustomName;
                    stock.GG = mingxi.guige;
                    stock.GH = mingxi.ganghao;
                    stock.houzhengli = mingxi.houzhengli;
                    stock.JS = 0 - mingxi.chengpingjuanshu;
                    stock.kaijianmishu = 0;
                    stock.kuanhao = mingxi.kuanhao;
                    stock.Kuwei = mingxi.Kuwei;
                    stock.KZ = mingxi.kezhong;
                    stock.MF = mingxi.menfu;
                    stock.MS = 0 - mingxi.chengpingmishu;
                    stock.orderNum = mingxi.OrderNum;
                    stock.own = danjuTable.own;
                    stock.PM = mingxi.pingming;
                    stock.RKDH = mingxi.danhao;
                    stock.RQ = danjuTable.rq;
                    stock.TotalMoney = 0 - mingxi.hanshuiheji;
                    stock.YS = mingxi.yanse;
                    stock.Huahao = mingxi.Huahao;
                    stock.ColorNum = mingxi.ColorNum;
                    stock.CustomerColorNum = mingxi.CustomerColorNum;
                    stock.CustomerPingMing = mingxi.CustomerPingMing;
                    stock.RKDH = mingxi.rkdh;
                    StockTableService.InsertStockTable(stock);
                }
                //如果已经存在
                else
                {
                    stock.MS -= mingxi.chengpingmishu;
                    stock.JS -= mingxi.chengpingjuanshu;
                    stock.TotalMoney -= mingxi.hanshuiheji;
                    if (stock.TotalMoney != 0 && stock.MS != 0)
                    {
                        stock.AvgPrice = (int)stock.TotalMoney / stock.MS;
                    }
                    else
                    {
                        stock.AvgPrice = 0;
                    }
                    //     StockTableService.UpdateStockTable(stock, x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.BH == mingxi.Bianhao && x.YS == mingxi.yanse  &&
                    //x.kuanhao == mingxi.kuanhao && x.houzhengli == mingxi.houzhengli && x.GH == mingxi.ganghao&&x.Kuwei==mingxi.Kuwei && x.Huahao == mingxi.Huahao && x.ColorNum == mingxi.ColorNum);
                    StockTableService.UpdateStockTable(stock, x => x.ID == stock.ID);
                }
                //减少订单明细的库存数(mingxi, danjuTable.ckmc);
            }

        }
        /// <summary>
        /// 增加生产入库库存
        /// </summary>
        /// <param name="danjumingxitables"></param>
        /// <param name="danjuTable"></param>
        public static void 增加入库库存(List<danjumingxitable> danjumingxitables, DanjuTable danjuTable)
        {
            foreach (var mingxi in danjumingxitables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.ckmc && x.YS == mingxi.yanse &&
                 x.GH == mingxi.ganghao && x.Kuwei == mingxi.Kuwei && x.PM == mingxi.pingming && x.CF == mingxi.chengfeng && x.KZ == mingxi.kezhong);
                //库存没有这个布  当编号为空的时候
                if (stock.ID == 0)
                {
                    stock.AvgPrice = mingxi.hanshuidanjia;
                    stock.BH = mingxi.Bianhao;
                    stock.biaoqianmishu = 0;
                    stock.CF = mingxi.chengfeng;
                    stock.CKMC = danjuTable.ckmc;
                    stock.ContractNum = mingxi.ContractNum;
                    stock.CustomName = mingxi.CustomName;
                    stock.GG = mingxi.guige;
                    stock.GH = mingxi.ganghao;
                    stock.houzhengli = mingxi.houzhengli;
                    stock.JS = mingxi.chengpingjuanshu;
                    stock.kaijianmishu = 0;
                    stock.kuanhao = mingxi.kuanhao;
                    stock.Kuwei = mingxi.Kuwei;
                    stock.KZ = mingxi.kezhong;
                    stock.MF = mingxi.menfu;
                    stock.MS = mingxi.chengpingmishu;
                    stock.orderNum = mingxi.OrderNum;
                    stock.own = danjuTable.own;
                    stock.PM = mingxi.pingming;
                    stock.RKDH = mingxi.danhao;
                    stock.RQ = danjuTable.rq;
                    stock.TotalMoney = mingxi.hanshuiheji;
                    stock.YS = mingxi.yanse;
                    stock.Huahao = mingxi.Huahao;
                    stock.ColorNum = mingxi.ColorNum;
                    stock.CustomerColorNum = mingxi.CustomerColorNum;
                    stock.CustomerPingMing = mingxi.CustomerPingMing;
                    stock.Rangchang = mingxi.Rangchang;
                    StockTableService.InsertStockTable(stock);
                }
                //如果已经存在
                else
                {
                    stock.MS += mingxi.chengpingmishu;
                    stock.JS += mingxi.chengpingjuanshu;
                    stock.TotalMoney -= mingxi.hanshuiheji;
                    if (stock.TotalMoney != 0 && stock.MS != 0)
                    {
                        stock.AvgPrice = (int)stock.TotalMoney / stock.MS;
                    }
                    else
                    {
                        stock.AvgPrice = 0;
                    }
                    //     StockTableService.UpdateStockTable(stock, x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.BH == mingxi.Bianhao && x.YS == mingxi.yanse  &&
                    //x.kuanhao == mingxi.kuanhao && x.houzhengli == mingxi.houzhengli && x.GH == mingxi.ganghao&&x.Kuwei==mingxi.Kuwei && x.Huahao == mingxi.Huahao && x.ColorNum == mingxi.ColorNum);
                    StockTableService.UpdateStockTable(stock, x => x.ID == stock.ID);
                }
                清零库存(stock);
            }
        }
        public static void 清零库存(StockTable stock)
        {
            if (stock.JS <= 0)
            {
                RukuTableService.InsertRukuTable(stock);
                Connect.dbcontext.Deleteable<StockTable>().Where(x => x.ID == stock.ID).ExecuteCommand();
            }
        }
        public static void 清零库存()
        {
            StockTableService.DeleteStockTable(x => x.RukuNum == 0);
        }
        public static void 清零库存(List<StockTable> stocks)
        {
            foreach (var s in stocks)
            {
                List<JuanHaoTable> juan = JuanHaoTableService.GetJuanHaoTablelst(x =>x.CustomerName==s.CustomName&& x.SampleName == s.PM && x.OrderNum == s.orderNum && x.Houzhengli == s.houzhengli && x.GangHao == s.GH && x.yanse == s.YS && x.kuanhao == s.kuanhao && x.state == 0);
                if (juan.Count >= 0)
                {
                    RukuTableService.InsertRukuTable(s);
                    var item = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == s.RKDH
                    && x.ganghao == s.GH
                    && x.yanse == s.YS
                    &&x.pingming==s.PM
                    );
                    if(item!=null)
                    {
                        item.toupimishu = s.MS;
                        item.toupijuanshu = s.JS;
                        item.InventoryQuantity = 0;
                        Connect.dbcontext.Updateable(item).ExecuteCommand();
                    }
                    StockTableService.DeleteStockTable(x => x.ID == s.ID);
                    DeleteLogService.InsertDeleteLog(new DeleteLog() { date=DateTime.Now , useID=CurrentUser.user.YHMC, Log=$"清零库存品名{s.PM },缸号{s.GH },颜色{s.YS },客户名{s.CustomName },米数{s.MS},后整理{s.houzhengli },入库单号{s.RKDH},流转卡号{s.LiuzhuanCard }"});
                }
                try
                {
                    juan.ForEach(x => x.state = 1);
                    Connect.dbcontext.Updateable(juan).ExecuteCommand();
                }
                catch
                {

                }
            }
        }
        public static void 减少库存(List<RangShequpiTable> rangShequpiTables, DanjuTable danjuTable)
        {
            foreach (var mingxi in rangShequpiTables)
            {
                var stock = Connect.dbcontext.Queryable<StockTable>().First (x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.BH == mingxi.SampleNum && x.YS == mingxi.color &&
                x.kuanhao == mingxi.kuanhao && x.houzhengli == mingxi.houzhengli && x.GH == mingxi.ganghao && x.Kuwei == mingxi.Remarkers && x.Huahao == mingxi.Huahao && x.ColorNum == mingxi.ColorNum);
                //库存没有这个布  当编号为空的时候
                if (stock==null)
                {
                    //stock.AvgPrice = 0;
                    //stock.BH = mingxi.SampleNum;
                    //stock.biaoqianmishu = 0;
                    //stock.CF = mingxi.chengfeng;
                    //stock.CKMC = danjuTable.ckmc;
                    //stock.ContractNum = mingxi.ContractNum;
                    //stock.CustomName = mingxi.CustomName;
                    //stock.GG = mingxi.Specifications;
                    //stock.GH = mingxi.ganghao;
                    //stock.houzhengli = mingxi.houzhengli;
                    //stock.JS = 0 - mingxi.ToupiJuanshu;
                    //stock.kaijianmishu = 0;
                    //stock.kuanhao = mingxi.kuanhao;
                    //stock.Kuwei = mingxi.Remarkers;
                    //stock.KZ = mingxi.kezhong;
                    //stock.MF = mingxi.menfu;
                    //stock.MS = 0 - mingxi.ToupiMishu;
                    //stock.orderNum = mingxi.OrderNum;
                    //stock.own = danjuTable.own;
                    //stock.PM = mingxi.sampleName;
                    //stock.RKDH = mingxi.CaigouDanhao;
                    //stock.RQ = danjuTable.rq;
                    //stock.TotalMoney = 0;
                    //stock.YS = mingxi.color;
                    //stock.Huahao = mingxi.Huahao;
                    //stock.ColorNum = mingxi.ColorNum;
                    //stock.CustomerColorNum = mingxi.CustomerColorNum;
                    //stock.CustomerPingMing = mingxi.CustomerPingMing;

                    //StockTableService.InsertStockTable(stock);
                }
                //如果已经存在
                else
                {
                    stock.MS -= mingxi.ToupiMishu;
                    stock.JS -= mingxi.ToupiJuanshu;
                    //stock.TotalMoney -= mingxi.ToupiMishu * stock.AvgPrice;
                    //if (stock.TotalMoney != 0 && stock.MS != 0)
                    //{
                    //    stock.AvgPrice = (int)stock.TotalMoney / stock.MS;
                    //}
                    //else
                    //{
                    //    stock.AvgPrice = 0;
                    //}
                    //     StockTableService.UpdateStockTable(stock, x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.BH == mingxi.SampleNum  && x.YS == mingxi.color  &&
                    //x.kuanhao == mingxi.kuanhao && x.houzhengli == mingxi.houzhengli && x.GH == mingxi.ganghao && x.Kuwei == mingxi.Remarkers && x.Huahao == mingxi.Huahao && x.ColorNum == mingxi.ColorNum);
                    Connect.dbcontext.Updateable(stock).ExecuteCommand ();
                    var danjumingxi = new danjumingxitable()
                    {
                        chengpingmishu = mingxi.ToupiMishu,
                        OrderNum = mingxi.OrderNum,
                        yanse = mingxi.color,
                        kuanhao = mingxi.kuanhao,
                        CustomerPingMing = mingxi.CustomerPingMing,
                        ColorNum = mingxi.ColorNum,
                        Bianhao = mingxi.SampleNum,
                    };
                }
                清零库存(stock);
            }

        }
        public static void 增加库存(List<RangShequpiTable> rangShequpiTables, DanjuTable danjuTable)
        {
            foreach (var mingxi in rangShequpiTables)
            {
                var stock = Connect.dbcontext.Queryable<StockTable>().First(x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.BH == mingxi.SampleNum && x.YS == mingxi.color &&
                x.kuanhao == mingxi.kuanhao && x.houzhengli == mingxi.houzhengli && x.GH == mingxi.ganghao && x.Kuwei == mingxi.Remarkers && x.Huahao == mingxi.Huahao && x.ColorNum == mingxi.ColorNum);
                //库存没有这个布  当编号为空的时候
                if (stock==null)
                {
                    stock = new StockTable
                    {
                        AvgPrice = mingxi.Danjia,
                        BH = mingxi.SampleNum,
                        biaoqianmishu = 0,
                        CF = mingxi.chengfeng,
                        CKMC = danjuTable.ckmc,
                        ContractNum = mingxi.ContractNum,
                        CustomName = mingxi.CustomName,
                        GG = mingxi.Specifications,
                        GH = mingxi.ganghao,
                        houzhengli = mingxi.houzhengli,
                        JS = mingxi.ToupiJuanshu,
                        kaijianmishu = 0,
                        kuanhao = mingxi.kuanhao,
                        Kuwei = mingxi.Remarkers,
                        KZ = mingxi.kezhong,
                        MF = mingxi.menfu,
                        MS = mingxi.ToupiMishu,
                        orderNum = mingxi.OrderNum,
                        own = danjuTable.own,
                        PM = mingxi.sampleName,
                        RKDH = mingxi.CaigouDanhao,
                        RQ = danjuTable.rq,
                        TotalMoney = mingxi.Heji,
                        YS = mingxi.color,
                        Huahao = mingxi.Huahao,
                        ColorNum = mingxi.ColorNum,
                        CustomerColorNum = mingxi.CustomerColorNum,
                        CustomerPingMing = mingxi.CustomerPingMing
                    };
                    //stock.RukuNum = mingxi.ToupiMishu;
                    Connect.dbcontext.Insertable(stock).ExecuteCommand();
                }
                //如果已经存在
                else
                {
                    stock.MS += mingxi.ToupiMishu;
                    stock.JS += mingxi.ToupiJuanshu;
                    Connect.dbcontext.Updateable( stock).ExecuteCommand();
                    var danjumingxi = new danjumingxitable()
                    {
                        chengpingmishu = mingxi.ToupiMishu,
                        OrderNum = mingxi.OrderNum,
                        yanse = mingxi.color,
                        kuanhao = mingxi.kuanhao,
                        CustomerPingMing = mingxi.CustomerPingMing,
                        ColorNum = mingxi.ColorNum,
                        Bianhao = mingxi.SampleNum,
                    };
                }
            }
        }
        public static void 增加库存(List<ChangliangTable> changliangTables)
        {
            foreach (var mingxi in changliangTables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.CKMC == mingxi.Ckmc && x.orderNum == "" && x.BH == mingxi.PingzhongBianhao && x.YS == "" &&
                x.kuanhao == mingxi.Pihao && x.houzhengli == "" && x.GH == "" && x.Kuwei == mingxi.Kuwei);
                //库存没有这个布  当编号为空的时候
                if (stock.ID == 0)
                {
                    stock.AvgPrice = 0;
                    stock.BH = mingxi.PingzhongBianhao;
                    stock.biaoqianmishu = 0;
                    stock.CF = "";
                    stock.CKMC = mingxi.Ckmc;
                    stock.ContractNum = "";
                    stock.CustomName = "";
                    stock.GG = mingxi.GuiGe;
                    stock.GH = "";
                    stock.houzhengli = "";
                    stock.JS = 1;
                    stock.kaijianmishu = 0;
                    stock.kuanhao = mingxi.Pihao;
                    stock.Kuwei = mingxi.Kuwei;
                    stock.KZ = "";
                    stock.MF = "";
                    stock.MS = mingxi.Shuliang;
                    stock.orderNum = "";
                    stock.own = mingxi.Yuangong;
                    stock.PM = mingxi.PingMing;
                    stock.RKDH = mingxi.Danhao;
                    stock.RQ = mingxi.Riqi;
                    stock.TotalMoney = 0;
                    stock.RukuNum = mingxi.Shuliang;
                    stock.YS = "";
                    StockTableService.InsertStockTable(stock);
                }
                //如果已经存在
                else
                {
                    stock.MS += mingxi.Shuliang;
                    stock.JS += 1;
                    stock.TotalMoney += 0;
                    stock.RukuNum += mingxi.Shuliang;
                    if (stock.TotalMoney != 0 && stock.MS != 0)
                    {
                        stock.AvgPrice = (int)stock.TotalMoney / stock.MS;
                    }
                    else
                    {
                        stock.AvgPrice = 0;
                    }
                    //     StockTableService.UpdateStockTable(stock, x => x.CKMC == mingxi .Ckmc && x.orderNum == "" && x.BH == mingxi.PingzhongBianhao  && x.YS =="" &&
                    //x.kuanhao == mingxi.Pihao  && x.houzhengli == "" && x.GH == "" && x.Kuwei == mingxi.Kuwei );
                    StockTableService.UpdateStockTable(stock, x => x.ID == stock.ID);
                }
            }
        }
        public static void 减少库存(List<ChangliangTable> changliangTables)
        {
            foreach (var mingxi in changliangTables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.CKMC == mingxi.Ckmc && x.orderNum == "" && x.BH == mingxi.PingzhongBianhao && x.YS == "" &&
                x.kuanhao == mingxi.Pihao && x.houzhengli == "" && x.GH == "" && x.Kuwei == mingxi.Kuwei);
                //库存没有这个布  当编号为空的时候
                if (stock.ID == 0)
                {
                    stock.AvgPrice = 0;
                    stock.BH = mingxi.PingzhongBianhao;
                    stock.biaoqianmishu = 0;
                    stock.CF = "";
                    stock.CKMC = mingxi.Ckmc;
                    stock.ContractNum = "";
                    stock.CustomName = "";
                    stock.GG = mingxi.GuiGe;
                    stock.GH = "";
                    stock.houzhengli = "";
                    stock.JS = -1;
                    stock.kaijianmishu = 0;
                    stock.kuanhao = mingxi.Pihao;
                    stock.Kuwei = mingxi.Kuwei;
                    stock.KZ = "";
                    stock.MF = "";
                    stock.MS = 0 - mingxi.Shuliang;
                    stock.orderNum = "";
                    stock.own = mingxi.Yuangong;
                    stock.PM = mingxi.PingMing;
                    stock.RKDH = mingxi.Danhao;
                    stock.RQ = mingxi.Riqi;
                    stock.TotalMoney = 0;
                    stock.YS = "";
                    StockTableService.InsertStockTable(stock);
                }
                //如果已经存在
                else
                {
                    stock.MS -= mingxi.Shuliang;
                    stock.JS -= 1;
                    stock.TotalMoney += 0;
                    if (stock.TotalMoney != 0 && stock.MS != 0)
                    {
                        stock.AvgPrice = (int)stock.TotalMoney / stock.MS;
                    }
                    else
                    {
                        stock.AvgPrice = 0;
                    }
                    //     StockTableService.UpdateStockTable(stock, x => x.CKMC == mingxi.Ckmc && x.orderNum == "" && x.BH == mingxi.PingzhongBianhao && x.YS == "" &&
                    //x.kuanhao == mingxi.Pihao && x.houzhengli == "" && x.GH == "" && x.Kuwei == mingxi.Kuwei);
                    StockTableService.UpdateStockTable(stock, x => x.ID == stock.ID);
                }
            }
        }
        /// <summary>
        /// 检查库存中是否有这个商品,返回False表示有布料没库存否则表示全部有库存
        /// </summary>
        /// <param name="danjumingxitables">单据</param>
        /// <param name="danjuTable"></param>
        /// <returns></returns>
        public static danjumingxitable 检查库存(List<danjumingxitable> danjumingxitables, DanjuTable danjuTable)
        {
            foreach (var mingxi in danjumingxitables.Where(x => x.Bianhao != null).ToList())
            {
                var stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.ckmc && x.orderNum == mingxi.OrderNum && x.BH == mingxi.Bianhao && x.YS == mingxi.yanse &&
                x.kuanhao == mingxi.kuanhao && x.houzhengli == mingxi.houzhengli && x.GH == mingxi.ganghao && x.Kuwei == mingxi.Kuwei && x.Huahao == mingxi.Huahao && x.ColorNum == mingxi.ColorNum);
                //库存没有这个布  当编号为空的时候
                if (stock.ID == 0)
                {
                    return mingxi;
                }
            }
            return new danjumingxitable();
        }
        public static RangShequpiTable 检查库存(List<RangShequpiTable> danjumingxitables, DanjuTable danjuTable)
        {
            foreach (var mingxi in danjumingxitables.Where(x => x.SampleNum != null).ToList())
            {
                var stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.ksmc && x.orderNum == mingxi.OrderNum && x.BH == mingxi.SampleNum && x.YS == mingxi.color &&
              x.kuanhao == mingxi.kuanhao && x.houzhengli == mingxi.houzhengli && x.GH == mingxi.ganghao && x.Kuwei == mingxi.Remarkers && x.Huahao == mingxi.Huahao && x.ColorNum == mingxi.ColorNum);
                //库存没有这个布  当编号为空的时候
                if (stock.BH == string.Empty)
                {
                    return mingxi;
                }
            }
            return new RangShequpiTable();
        }
        public static void 增加库存(StockTable stocktable)
        {
            var stock = StockTableService.GetOneStockTable(x => x.CKMC == stocktable.CKMC && x.orderNum == stocktable.orderNum && x.BH == stocktable.BH && x.YS == stocktable.YS &&
              x.kuanhao == stocktable.kuanhao && x.houzhengli == stocktable.houzhengli && x.GH == stocktable.GH && x.Kuwei == stocktable.Kuwei && x.Huahao == stocktable.Huahao);
            //库存没有这个布  当编号为空的时候
            if (stock.ID == 0)
            {
                stocktable.Difference = stocktable.RukuNum - stocktable.yijianmishu;
                StockTableService.InsertStockTable(stocktable);
            }
            //如果已经存在
            else
            {
                stock.MS += stocktable.MS;
                stock.JS += stocktable.JS;
                stock.biaoqianmishu += stock.biaoqianmishu;
                stock.yijianjuanshu += stock.yijianjuanshu;
                stock.yijianmishu += stock.yijianmishu;
                stock.TotalMoney += stocktable.TotalMoney;
                stock.RukuNum += stocktable.MS;
                stock.Difference = stock.RukuNum - stock.yijianmishu;
                StockTableService.UpdateStockTable(stock, x => x.ID == stock.ID);
            }

        }
        public static void 减少库存(StockTable stocktable)
        {
            var stock = StockTableService.GetOneStockTable(x => x.CKMC == stocktable.CKMC && x.orderNum == stocktable.orderNum && x.BH == stocktable.BH && x.YS == stocktable.YS &&
              x.kuanhao == stocktable.kuanhao && x.houzhengli == stocktable.houzhengli && x.GH == stocktable.GH && x.Kuwei == stocktable.Kuwei && x.Huahao == stocktable.Huahao);
            //库存没有这个布  当编号为空的时候
            if (stock.ID == 0)
            {
                StockTableService.InsertStockTable(stocktable);
            }
            //如果已经存在
            else
            {
                stock.MS -= stocktable.MS;
                stock.JS -= stocktable.JS;
                stock.biaoqianmishu -= stock.biaoqianmishu;
                stock.yijianjuanshu -= stock.yijianjuanshu;
                stock.yijianmishu -= stock.yijianmishu;
                stock.RukuNum = stocktable.RukuNum;
                stock.Difference = stock.RukuNum - stock.yijianmishu;
                StockTableService.UpdateStockTable(stock, x => x.ID == stock.ID);
            }
        }
        public static void 增加发货数量(DanjuTable danjuTable, List<danjumingxitable> danjumingxitables)
        {
            foreach (var mingxi in danjumingxitables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.ID == mingxi.suolv);
                if (stock.ID > 0)
                {
                    stock.YiFaNum += mingxi.toupimishu;
                    StockTableService.UpdateStockTable(x => x.YiFaNum == stock.YiFaNum, x => x.ID == stock.ID);
                }
            }
        }
        public static void 减少发货数量(DanjuTable danjuTable, List<danjumingxitable> danjumingxitables)
        {
            foreach (var mingxi in danjumingxitables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.ID == mingxi.suolv);
                if (stock.ID > 0)
                {
                    stock.YiFaNum -= mingxi.toupimishu;
                    StockTableService.UpdateStockTable(stock, x => x.ID == stock.ID);
                }
            }
        }
        public static void 检验完毕(int id)
        {
            var stock = StockTableService.GetOneStockTable(x => x.ID == id);
            StockTableService.UpdateStockTable($"IsCheckDone='已结束',MS='{stock.biaoqianmishu }'", x => x.ID == stock.ID);
        }
        public static void 检验未完毕(int id)
        {
            var stock = StockTableService.GetOneStockTable(x => x.ID == id);
            stock.IsCheckDone = "未结束";
            StockTableService.UpdateStockTable(stock, x => x.ID == stock.ID);
        }
        /// <summary>
        /// 存在架子号返回True，否则返回False
        /// </summary>
        /// <param name="jiazhihao"></param>
        /// <returns></returns>
        public static Boolean 检查架子号(string jiazhihao)
        {
            var jiazhis = jiazhihao.Split('-');
            return JiaziTableService.GetJiaziTablelst(x => x.Guisuo == jiazhis[1] && x.JiaziID == jiazhis[0]).Count > 0;
        }
        public static void 增加入库数量(DanjuTable danjuTable, List<danjumingxitable> danjumingxitables)
        {
            foreach (var mingxi in danjumingxitables)
            {
                var stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.ckmc && x.YS == mingxi.yanse && x.RKDH == mingxi.rkdh &&
                    x.GH == mingxi.ganghao && x.Kuwei == mingxi.Kuwei && x.PM == mingxi.pingming && x.CF == mingxi.chengfeng && x.KZ == mingxi.kezhong && x.CustomName == mingxi.CustomName);
                if (!string.IsNullOrEmpty(stock.PM))
                {
                    stock.RukuNum += mingxi.chengpingmishu;
                    stock.Difference += mingxi.chengpingmishu;
                    StockTableService.UpdateStockTable($"RukuNum='{stock.RukuNum }',Difference='{stock.Difference }'", x => x.ID == stock.ID);
                }
            }
        }
        public static void 拆分布料(Liuzhuancard danjuTable, danjumingxitable mingxi)
        {
            var stock = StockTableService.GetOneStockTable(x => x.CKMC == danjuTable.StockName && x.YS == mingxi.yanse && x.RKDH == mingxi.rkdh &&
                x.GH == mingxi.ganghao && x.PM == mingxi.pingming && x.CustomName == mingxi.CustomName);
            stock.RukuNum -= mingxi.chengpingmishu;
            stock.MS -= mingxi.chengpingmishu;
            stock.JS -= mingxi.chengpingjuanshu;
            stock.Difference = stock.RukuNum - stock.yijianmishu;
            int stockid = stock.ID;
            StockTableService.UpdateStockTable(x => x.MS == stock.MS && x.JS == stock.JS && x.RukuNum == stock.RukuNum && x.Difference == stock.Difference, y => y.ID == stock.ID);
            ///新增一条库存信息
            stock = new StockTable
            {
                AvgPrice = stockid,
                BH = mingxi.Bianhao,
                biaoqianmishu = 0,
                CF = mingxi.chengfeng,
                CKMC = danjuTable.StockName,
                ContractNum = mingxi.ContractNum,
                CustomName = mingxi.CustomName,
                GG = mingxi.guige,
                GH = mingxi.ganghao,
                houzhengli = mingxi.houzhengli,
                JS = mingxi.chengpingjuanshu,
                kaijianmishu = 0,
                kuanhao = mingxi.kuanhao,
                Kuwei = mingxi.Kuwei,
                KZ = mingxi.kezhong,
                MF = mingxi.menfu,
                MS = mingxi.chengpingmishu,
                orderNum = mingxi.OrderNum,
                own = danjuTable.own,
                PM = mingxi.pingming,
                RKDH = mingxi.danhao,
                RQ = danjuTable.Card_Date,
                TotalMoney = mingxi.hanshuiheji,
                YS = mingxi.yanse,
                Huahao = mingxi.Huahao,
                ColorNum = mingxi.ColorNum,
                CustomerColorNum = mingxi.CustomerColorNum,
                CustomerPingMing = mingxi.CustomerPingMing,
                Rangchang = mingxi.Rangchang,
                Difference = mingxi.chengpingmishu,
                RukuNum = mingxi.chengpingmishu
            };
            StockTableService.InsertStockTable(stock);
        }
        public static void 合并布料(string liuzhuancardNum)
        {
            var stocks = StockTableService.GetStockTablelst(x => x.LiuzhuanCard == liuzhuancardNum);
            foreach (var stock in stocks)
            {
                if (stock.AvgPrice > 0)
                {
                    ///还回去
                    var s = StockTableService.GetOneStockTable(x => x.ID == stock.AvgPrice);
                    s.RukuNum += stock.RukuNum;
                    s.MS += stock.MS;
                    s.JS += stock.JS;
                    s.biaoqianmishu += stock.biaoqianmishu;
                    s.yijianmishu += stock.yijianmishu;
                    s.Difference = stock.RukuNum - stock.yijianmishu;
                    Connect.dbcontext.Updateable(s).Where(x => x.ID == stock.AvgPrice).ExecuteCommand();
                    //StockTableService.UpdateStockTable(x => x.Difference == s.Difference && x.yijianmishu == s.yijianmishu && x.RukuNum == s.RukuNum && x.MS == s.MS && x.JS == s.JS && x.biaoqianmishu == s.biaoqianmishu,
                    //    y => y.ID == stock.AvgPrice);
                    ///删除信息
                    StockTableService.DeleteStockTable(x => x.ID == stock.ID);
                }
            }
        }
        public static void 原料出库(List<ImportAndExportOfRawMaterials> rawMaterials)
        {
            rawMaterials = rawMaterials.Where(x => !string.IsNullOrEmpty(x.ProductName) && x.CalculateInventory == "是").ToList();
            foreach (var raw in rawMaterials)
            {
                var r = Connect.dbcontext.Queryable<RawMaterial>().First(x => x.Num == raw.Num);
                if (r != null)
                {
                    r.InventoryQuantity -= raw.InventoryQuantity;
                    r.InventoryQuantity1 -= raw.InventoryQuantity1;
                    计算可用原料库存(r);
                    r.Kilograms -= raw.Kilograms;
                    Connect.dbcontext.Updateable<RawMaterial>(r).ExecuteCommand();
                }
            }
        }
        public static void 原料入库(List<ImportAndExportOfRawMaterials> rawMaterials)
        {
            rawMaterials = rawMaterials.Where(x => !string.IsNullOrEmpty(x.ProductName) && x.CalculateInventory == "是").ToList();
            try
            {
                foreach (var raw in rawMaterials)
                {
                    var r = Connect.dbcontext.Queryable<RawMaterial>().First(x => x.Num == raw.Num);
                    if (r != null)
                    {
                        r.InventoryQuantity += raw.InventoryQuantity;
                        计算可用原料库存(r);
                        r.InventoryQuantity1+= raw.InventoryQuantity1;
                        r.Kilograms += raw.Kilograms;
                        Connect.dbcontext.Updateable<RawMaterial>(r).ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void 原料生产出库(List<ImportAndExportOfRawMaterials> rawMaterials)
        {
            rawMaterials = rawMaterials.Where(x => !string.IsNullOrEmpty(x.ProductName) && x.CalculateInventory == "是").ToList();
            try
            {
                foreach (var raw in rawMaterials)
                {
                    var r = Connect.dbcontext.Queryable<RawMaterial>().First(x => x.Num == raw.Num);
                    if (r != null)
                    {
                        r.PreOutboundQuantity -= raw.InventoryQuantity;                     
                        r.InventoryQuantity-= raw.InventoryQuantity;
                        计算可用原料库存(r);
                        r.InventoryQuantity1 -= raw.InventoryQuantity1;
                        r.Kilograms -= raw.Kilograms;
                        Connect.dbcontext.Updateable<RawMaterial>(r).ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void 原料生产入库(List<ImportAndExportOfRawMaterials> rawMaterials)
        {
            rawMaterials = rawMaterials.Where(x => !string.IsNullOrEmpty(x.ProductName) && x.CalculateInventory == "是").ToList();
            try
            {
                foreach (var raw in rawMaterials)
                {
                    var r = Connect.dbcontext.Queryable<RawMaterial>().First(x => x.Num == raw.Num);
                    if (r != null)
                    {
                        r.PreOutboundQuantity += raw.InventoryQuantity;                      
                        r.InventoryQuantity += raw.InventoryQuantity;
                        计算可用原料库存(r);
                        r.InventoryQuantity1 += raw.InventoryQuantity1;
                        r.Kilograms += raw.Kilograms;
                        Connect.dbcontext.Updateable<RawMaterial>(r).ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void 原料预入库(List<ImportAndExportOfRawMaterials> rawMaterials)
        {
            rawMaterials = rawMaterials.Where(x => !string.IsNullOrEmpty(x.ProductName) && x.CalculateInventory == "是").ToList();
            try
            {
                foreach (var raw in rawMaterials)
                {
                    var r = Connect.dbcontext.Queryable<RawMaterial>().First(x => x.Num == raw.Num);
                    if (r != null)
                    {
                        r.PreOutboundQuantity  -= raw.InventoryQuantity;
                        计算可用原料库存(r);
                        Connect.dbcontext.Updateable<RawMaterial>(r).ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void 原料预出库(List<ImportAndExportOfRawMaterials> rawMaterials)
        {
            rawMaterials = rawMaterials.Where(x => !string.IsNullOrEmpty(x.ProductName) && x.CalculateInventory == "是").ToList();
            try
            {
                foreach (var raw in rawMaterials)
                {
                    var r = Connect.dbcontext.Queryable<RawMaterial>().First(x => x.Num == raw.Num);
                    if (r != null)
                    {
                        r.PreOutboundQuantity  += raw.InventoryQuantity;
                        计算可用原料库存(r);
                        Connect.dbcontext.Updateable<RawMaterial>(r).ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private static void 计算可用原料库存(RawMaterial r)
        {
            r.CanUsedQuantity = r.InventoryQuantity - r.PreOutboundQuantity;
        }
        public static List<StockTable> 获取库存信息(danjumingxitable item)
        {
            return Connect.dbcontext.Queryable<StockTable>().Where(x => x.RKDH == item.rkdh && x.GH == item.ganghao && x.YS == item.yanse &&
            x.PM == item.pingming && x.orderNum == item.OrderNum).ToList();
        }
        public static InventoryQuantity 获取库存数(danjumingxitable item)
        {
            var stocks= 获取库存信息(item);
            return new InventoryQuantity() { Quantity = stocks.Sum(x => x.MS), Rolls = stocks.Sum(x => x.JS) };
        }
    }
}
